package com.bootdo.crm.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.crm.domain.LeadsDO;
import com.bootdo.crm.service.LeadsService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 线索表
 * 
 * @author cui
 * @email 1992lcg@163.com
 * @date 2020-02-20 13:14:28
 */
 
@Controller
@RequestMapping("/crm/leads")
public class LeadsController {
	@Autowired
	private LeadsService leadsService;
	
	@GetMapping()
	@RequiresPermissions("crm:leads:leads")
	String Leads(){
	    return "crm/leads/leads";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("crm:leads:leads")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<LeadsDO> leadsList = leadsService.list(query);
		int total = leadsService.count(query);
		PageUtils pageUtils = new PageUtils(leadsList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("crm:leads:add")
	String add(){
	    return "crm/leads/add";
	}

	@GetMapping("/edit/{leadsId}")
	@RequiresPermissions("crm:leads:edit")
	String edit(@PathVariable("leadsId") Integer leadsId,Model model){
		LeadsDO leads = leadsService.get(leadsId);
		model.addAttribute("leads", leads);
	    return "crm/leads/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("crm:leads:add")
	public R save( LeadsDO leads){
		if(leadsService.save(leads)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("crm:leads:edit")
	public R update( LeadsDO leads){
		leadsService.update(leads);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("crm:leads:remove")
	public R remove( Integer leadsId){
		if(leadsService.remove(leadsId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("crm:leads:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] leadsIds){
		leadsService.batchRemove(leadsIds);
		return R.ok();
	}
	
}
